﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Interop;
using Win32;

namespace BaseUi.APP
{
    internal class APPBLL
    {
        private int numb = 0;
        IntPtr hwnd1;
        static string formTitle = "Nuvoton NuMicro ICP Programming Tool 3.00";
        public static void CloseApp()
        {
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains("ICP Programming Tool"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
        }
        public void AppStart(System.Windows.Forms.Panel panel, Dictionary<string, string> dic)
        {
            dic["窗体名"] = "MultiAMPT_V2.0.4";
            dic["程序路径"] = "D:\\桌面文件夹\\Desktop\\log文件\\307+\\MultiAMPT V2.0.4 Release-0724\\MultiAMPT V2.0.4 Release";
            dic["程序名"] = "MultiAMPT.exe";
            dic["线程名"] = "MultiAMPT";
            Process process1 = new Process();
            string formTitle = dic["窗体名"];
            string appPath = dic["程序路径"];
            string appName = dic["程序名"];
            string app1Name = dic["线程名"];

            numb = -20;
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains(app1Name))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
            process1.StartInfo.WorkingDirectory = appPath;
            process1.StartInfo.FileName = appName;
            string path = appPath;
            process1.Start();
            Thread.Sleep(1000);
            IntPtr ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            while (ParentHandle == IntPtr.Zero)
                ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            hwnd1 = ParentHandle;
            FormInsert1(ParentHandle, panel);
        }
        public void AppStart1(System.Windows.Forms.Panel panel, Dictionary<string, string> dic)
        {
            Process process1 = new Process();
            string formTitle = dic["窗体名"];
            string appPath = dic["程序路径"];
            string appName = dic["程序名"];
            string app1Name = dic["线程名"];

            //numb = (-1) * int.Parse(dic["上移"]);
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains(app1Name))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
            process1.StartInfo.WorkingDirectory = appPath;
            process1.StartInfo.FileName = appName;
            string path = appPath;
            process1.Start();
            Thread.Sleep(1000);
            IntPtr ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            while (ParentHandle == IntPtr.Zero)
                ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            hwnd1 = ParentHandle;
            FormInsert1(ParentHandle, panel);
        }
        public void FormInsert1(IntPtr hwnd, System.Windows.Forms.Panel panel)
        {
            User.SetParent(hwnd, panel.Handle);
            User.SetWindowLong(hwnd, User.GWL_STYLE, User.WS_VISIBLE);
            User.MoveWindow(hwnd, 0, 0, panel.Width, panel.Height, 1);
            User.ShowWindow(hwnd, User.SW_SHOWDEFAULT);
        }
        public void MovePand1(System.Windows.Forms.Panel panel)
        {
            User.MoveWindow(hwnd1, 0, numb, panel.Width, panel.Height, 1);
        }
        public void GetAPP(Form form, System.Windows.Forms.Panel panel, Dictionary<string, string> dic)
        {
            Process process1 = new Process();
            string formTitle = dic["窗体名"];
            string appPath = dic["程序路径"];
            string appName = dic["程序名"];
            process1.StartInfo.WorkingDirectory = appPath;
            process1.StartInfo.FileName = appName;

            Process[] myproc = Process.GetProcesses();
            

            process1.Start();

            IntPtr ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            while (ParentHandle == IntPtr.Zero)
                ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            FormInsert(ParentHandle, panel);
            form.Visible = true;
            form.WindowState = FormWindowState.Maximized;
            //   }
        }
        public void FormInsert(IntPtr hwnd, System.Windows.Forms.Panel panel)
        {
            User.SetParent(hwnd, panel.Handle);
            User.SetWindowLong(hwnd, User.GWL_STYLE, User.WS_VISIBLE);
            User.MoveWindow(hwnd, 0, 0, panel.Width, panel.Height, 1);
            User.ShowWindow(hwnd, User.SW_SHOWDEFAULT);
        }

    }
}
